﻿using System;
using System.Collections.Generic;

using RexToy;
using RexToy.DesignPattern;
using RexToy.UI.WinForm;
using RexToy.UI.WinForm.Pmlx;
using RexToy.UI.WinForm.ViewModel;

namespace UnitTest.WinForm.SampleControl
{
    public class LoginFormVM : ViewModelBase
    {
        Model.LoginInfo _info;
        public LoginFormVM()
        {
            _info = new Model.LoginInfo();
            _info.Name = "Name";
            _info.Password = "Pwd";
        }

        public string Name
        {
            get { return _info.Name; }
            set
            {
                if (_info.Name == value)
                    return;
                _info.Name = value;

                base.OnPropertyChange<Model.LoginInfo>(i => i.Name);
            }
        }

        public string Password
        {
            get { return _info.Password; }
            set
            {
                if (_info.Password == value)
                    return;
                _info.Password = value;
                base.OnPropertyChange<Model.LoginInfo>(i => i.Password);
            }
        }

        private IUICommand _okCmd;
        public IUICommand OKCommand
        {
            get
            {
                if (_okCmd == null)
                {
                    //_info.Name/_info.Password, logic
                    _okCmd = new ActionCommand(() => this.Login());
                }
                return _okCmd;
            }
        }

        private IUICommand _cancelCmd;
        public IUICommand CancelCommand
        {
            get
            {
                if (_cancelCmd == null)
                {
                    _cancelCmd = new ActionCommand(() => this.Cancel());
                }
                return _cancelCmd;
            }
        }

        private void Login()
        {
            Console.WriteLine("Login clicked with {0}/{1}!!!", _info.Name, _info.Password);
            if (_info.Name == "sa" && _info.Password == "sa")
            {
                this.OnPropertyChange("LoggedIn");
            }

            this.OnClose();
        }

        private void Cancel()
        {
            Console.WriteLine("Cancel clicked!!!");
            this.OnClose();
        }
    }
}
